package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseModelMapper;
import com.ruoyi.base.domain.BaseModel;
import com.ruoyi.base.service.IBaseModelService;

import javax.annotation.PostConstruct;

/**
 * 型号Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-20
 */
@Service
public class BaseModelServiceImpl implements IBaseModelService 
{

    public static final String CACHE_MAP = "base_model";
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BaseModelMapper baseModelMapper;


    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }

    /**
     * 查询型号
     *
     * @param cateId 分类主键
     * @param name 型号名称
     * @return 型号
     */
    @Override
    public BaseModel selectBaseModelById(Long cateId, String name){
        String key = cateId+"_"+name;
        Long modelId = redisCache.getCacheMapValue(CACHE_MAP, name);
        if(modelId != null){
            return selectBaseModelById(modelId);
        }

        BaseModel baseModel = baseModelMapper.selectBaseModelByName(cateId, name);

        if(baseModel != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseModel.getId());
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+baseModel.getId(), baseModel);
        }
        return baseModel;
    }

    /**
     * 查询型号
     * 
     * @param id 型号主键
     * @return 型号
     */
    @Override
    public BaseModel selectBaseModelById(Long id)
    {
        String key = "id_"+id;
        BaseModel baseModel = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(baseModel != null){
            return baseModel;
        }

        baseModel = baseModelMapper.selectBaseModelById(id);

        if(baseModel != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseModel);
            redisCache.setCacheMapValue(CACHE_MAP, baseModel.getCateId()+"_"+baseModel.getName(), baseModel.getId());
        }
        return baseModel;
    }

    /**
     * 查询型号列表
     * 
     * @param baseModel 型号
     * @return 型号
     */
    @Override
    public List<BaseModel> selectBaseModelList(BaseModel baseModel)
    {
        return baseModelMapper.selectBaseModelList(baseModel);
    }

    /**
     * 新增型号
     * 
     * @param baseModel 型号
     * @return 结果
     */
    @Override
    public int insertBaseModel(BaseModel baseModel)
    {
        BaseModel old = baseModelMapper.selectBaseModelByName(baseModel.getCateId(), baseModel.getName());
        if(old != null){
            throw new ServiceException(baseModel.getName() + " 型号已存在");
        }
        baseModel.setCreateTime(DateUtils.getNowDate());
        return baseModelMapper.insertBaseModel(baseModel);
    }

    /**
     * 修改型号
     * 
     * @param baseModel 型号
     * @return 结果
     */
    @Override
    public int updateBaseModel(BaseModel baseModel)
    {
        BaseModel old = baseModelMapper.selectBaseModelByName(baseModel.getCateId(), baseModel.getName());
        if(old != null && !old.getId().equals(baseModel.getId())){
            throw new ServiceException(baseModel.getName() + " 型号已存在");
        }

        baseModel.setUpdateTime(DateUtils.getNowDate());
        return baseModelMapper.updateBaseModel(baseModel) + reCache();
    }

    /**
     * 批量删除型号
     * 
     * @param ids 需要删除的型号主键
     * @return 结果
     */
    @Override
    public int deleteBaseModelByIds(Long[] ids)
    {
        return baseModelMapper.deleteBaseModelByIds(ids) + reCache();
    }

    /**
     * 删除型号信息
     * 
     * @param id 型号主键
     * @return 结果
     */
    @Override
    public int deleteBaseModelById(Long id)
    {
        return baseModelMapper.deleteBaseModelById(id) + reCache();
    }
}
